package com.lxmes.sales.controller;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.lxmes.base.domain.JcItembzinfo;
import com.lxmes.common.utils.SecurityUtils;
import com.lxmes.sales.domain.NcClientFiles;
import com.lxmes.sales.service.INcClientFilesService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.lxmes.common.annotation.Log;
import com.lxmes.common.core.controller.BaseController;
import com.lxmes.common.core.domain.AjaxResult;
import com.lxmes.common.enums.BusinessType;
import com.lxmes.common.utils.poi.ExcelUtil;
import com.lxmes.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 客户档案Controller
 * 
 * @author ruoyi
 * @date 2022-11-22
 */
@RestController
@RequestMapping("/sales/files")
public class NcClientFilesController extends BaseController
{
    @Autowired
    private INcClientFilesService ncClientFilesService;

    /**
     * 查询客户档案列表
     */
    @PreAuthorize("@ss.hasPermi('sales:files:list')")
    @GetMapping("/list")
    public TableDataInfo list(NcClientFiles ncClientFiles)
    {
        startPage();
        List<NcClientFiles> list = ncClientFilesService.selectNcClientFilesList(ncClientFiles);
        return getDataTable(list);
    }
    @PreAuthorize("@ss.hasPermi('sales:files:list')")
    @GetMapping("/listAll")
    public TableDataInfo listAll(NcClientFiles ncClientFiles)
    {
        List<NcClientFiles> list = ncClientFilesService.selectNcClientFilesList(ncClientFiles);
        return getDataTable(list);
    }

    /**
     * 导出客户档案列表
     */
    @PreAuthorize("@ss.hasPermi('sales:files:export')")
    @Log(title = "客户档案", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, NcClientFiles ncClientFiles)
    {
        List<NcClientFiles> list = ncClientFilesService.selectNcClientFilesList(ncClientFiles);
        ExcelUtil<NcClientFiles> util = new ExcelUtil<NcClientFiles>(NcClientFiles.class);
        util.exportExcel(response, list, "客户档案数据");
    }

    /**
     * 获取客户档案详细信息
     */
    @PreAuthorize("@ss.hasPermi('sales:files:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(ncClientFilesService.selectNcClientFilesById(id));
    }

    @PreAuthorize("@ss.hasPermi('sales:files:query')")
    @GetMapping(value = "/getInfoByCode/{code}")
    public AjaxResult getInfoByCode(@PathVariable("code") String code)
    {
        return AjaxResult.success(ncClientFilesService.selectNcClientFilesByCode(code));
    }

    /**
     * 新增客户档案
     */
    @PreAuthorize("@ss.hasPermi('sales:files:add')")
    @Log(title = "客户档案", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody NcClientFiles ncClientFiles)
    {
        return toAjax(ncClientFilesService.insertNcClientFiles(ncClientFiles));
    }

    /**
     * 修改客户档案
     */
    @PreAuthorize("@ss.hasPermi('sales:files:edit')")
    @Log(title = "客户档案", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody NcClientFiles ncClientFiles)
    {
        return toAjax(ncClientFilesService.updateNcClientFiles(ncClientFiles));
    }

    /**
     * 删除客户档案
     */
    @PreAuthorize("@ss.hasPermi('sales:files:remove')")
    @Log(title = "客户档案", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(ncClientFilesService.deleteNcClientFilesByIds(ids));
    }

    @Log(title = "客户管理", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<NcClientFiles> util = new ExcelUtil<>(NcClientFiles.class);
        List<NcClientFiles> clientFilesList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = ncClientFilesService.imporIteminfo(clientFilesList, updateSupport, operName);

        return AjaxResult.success(message);
    }
    // 下载模板
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) throws IOException
    {
        ExcelUtil<NcClientFiles> util = new ExcelUtil<>(NcClientFiles.class);
        util.importTemplateExcel(response,"客户数据");
    }

}
